//
//  RSUmenManager.swift
//  MiTao
//
//  Created by zhang qiang on 2018/12/12.
//  Copyright © 2018 DDC. All rights reserved.
//

import Foundation

enum  PlatformType: Int{
    
    case wechatType = 1
    
    case qqType = 3
    
    case sinaType = 4
    
    case WechatTimeLine = 2
    
    case platformUnKnow = -1
    
    case qqZone = 7
}

enum ShareType: Int {
    case shareText
    
    case shareImage
    
    case shareImageAndText
    
    case shareWeb
    
    case shareMusic
    
    case shareVideo
    
    case sharUnKnow
}

class MTShareManager: NSObject {
    
    @objc class func confitUShareSettings() {
        
        UMSocialGlobal.shareInstance().isUsingHttpsWhenShareContent = false
        UMConfigure.initWithAppkey(AppConfig.umengAppKey, channel: nil)
        #if DEBUG
        UMConfigure.setLogEnabled(true)
        #endif
        configUSharePlatforms()
    }
    
    
    private class func configUSharePlatforms() {
        
        UMSocialManager.default().setPlaform(.wechatSession, appKey: AppConfig.wechatAppKey, appSecret: AppConfig.wechatAppSecret, redirectURL: AppConfig.wechatRedirectURL)
        
        UMSocialManager.default().removePlatformProvider(with: .wechatFavorite)
        
        UMSocialManager.default().setPlaform(.QQ, appKey: AppConfig.qqAppkey, appSecret: AppConfig.qqAppSecret, redirectURL: AppConfig.qqRedirectURL)
        
        UMSocialManager.default().setPlaform(.sina, appKey: AppConfig.sinaAppKey, appSecret: AppConfig.sinaAppSecret, redirectURL: AppConfig.sinaRedirectURL)
        
    }
    @objc class func configUMGoBack(url: URL) -> Bool {
        
        return UMSocialManager.default().handleOpen(url)
    }
    
    class func share(platformtype: PlatformType, sharemodel: ShareModel, shareType: ShareType ){
        
        var platform: UMSocialPlatformType?
        
        switch platformtype {
        case .wechatType:
            platform = .wechatSession
            break
        case .qqType:
            platform = .QQ
            break
        case .qqZone:
            platform = .qzone
            break
        case .sinaType:
            platform = .sina
            break
        case .WechatTimeLine:
            platform = .wechatTimeLine
            break
        default:
            return
        }
        
        let messageObject = UMSocialMessageObject.init()
        
        switch shareType {
            
        /// 只分享文字
        case .shareText:
            messageObject.text = sharemodel.text
            break
            
        /// 分享图片
        case .shareImage:
            let shareObject = UMShareImageObject.init()

            if let obj = sharemodel.shareImageObj {
                shareObject.shareImage = obj
            } else if let _ = sharemodel.thumbImage {
                shareObject.thumbImage = UIImage(named: sharemodel.thumbImage!)
            }
            if let _ = sharemodel.imageUrl{
                shareObject.shareImage = sharemodel.imageUrl
            }
            messageObject.shareObject = shareObject
            break
            
        /// 分享图文(支持新浪微博)
        case .shareImageAndText:
            messageObject.text = sharemodel.text
            let shareObject = UMShareImageObject.init()
            if let _ = sharemodel.thumbImage{
                shareObject.thumbImage = UIImage(named: sharemodel.thumbImage!)
            }
            if let _ = sharemodel.imageUrl{
                shareObject.shareImage = sharemodel.imageUrl
            }
            messageObject.shareObject = shareObject
            break
            
            
        /// 分享网络连接
        case .shareWeb:
            
            let shareObject = UMShareWebpageObject.shareObject(withTitle: sharemodel.title, descr: sharemodel.desc, thumImage: thumImage(shareModel: sharemodel))
            shareObject?.webpageUrl = sharemodel.webUrl
            messageObject.shareObject = shareObject
            break
            
            
        /// 分享音乐
        case .shareMusic:
            let shareObject = UMShareMusicObject.shareObject(withTitle: sharemodel.title, descr: sharemodel.desc, thumImage: thumImage(shareModel: sharemodel))
            shareObject?.musicUrl = sharemodel.musicUrl
            messageObject.shareObject = shareObject
            break
            
        /// 分享视频URL
        case .shareVideo:
            let shareObject = UMShareVideoObject.shareObject(withTitle: sharemodel.title, descr: sharemodel.desc, thumImage: thumImage(shareModel: sharemodel))
            shareObject?.videoUrl = sharemodel.videoUrl
            messageObject.shareObject = shareObject
            break
        default:
            return
            //            break
        }
        
        //一定不为空了
        UMSocialManager.default().share(to: platform!, messageObject: messageObject, currentViewController: nil) { (data, error) in
            
        }
    }
    
    /// 链接甄别系统
    class func thumImage(shareModel: ShareModel) -> Any {
        let imageStr: String = shareModel.thumbImage ?? shareModel.imageUrl ?? "Icon-60"
        if imageStr.hasPrefix("http://") || imageStr.hasPrefix("https://") {
            return imageStr
        }
        return UIImage(named: imageStr) as Any
    }
    
    class func getAuthWithUserInfoFromWechat(type: PlatformType, complete:@escaping (_ finish: Bool) -> ()){
        
        var platform: UMSocialPlatformType?
        
        switch type {
        case .wechatType:
            platform = .wechatSession
            break
        case .qqType:
            platform = .QQ
            break
        case .qqZone:
            platform = .qzone
            break
        default:
            platform = .sina
            break
        }
        
        UMSocialManager.default().getUserInfo(with: platform!, currentViewController: nil) { (result, error) in
            if (error != nil){
                // 授权失败
                complete(false)
            }else{
                //let resp = result as! UMSocialUserInfoResponse
                //debugPrint(resp.iconurl)
                // resp.uid
                // iconurl
                // resp.uid
                complete(true)
            }
        }
    }
    
}

class ShareModel {
    //文本
    @objc var text: String?
    
    /// 缩略图名字,本地的
    @objc var thumbImage: String?
    
    /// 分享图片的url.string
    @objc var imageUrl: String?
    
    /// 分享网页的url.string
    @objc var webUrl: String?
    
    /// 分享的标题
    @objc var title: String?
    
    /// 分享内容描述
    @objc var desc: String?
    
    /// 分享音乐url.string
    @objc var musicUrl: String?
    
    /// 分享的视频url.string
    @objc var videoUrl: String?
    
    /// 分享的图片
    @objc var shareImageObj: UIImage?
    
}
